﻿using System.Text.RegularExpressions;
using WF_AppFuel.Validation.Attributes;
using WF_AppFuel.Validation.Base;

namespace WF_AppFuel.Validation.Engines
{
    /// <summary>
    /// This engine is used for validating properties marked with the
    /// <code>MatchesPattern</code> validation attribute.
    /// </summary>
    /// <seealso cref="MatchesPattern" />
    public sealed class MatchesPatternValidationEngine : BaseValidationEngine
    {
        public override bool Run()
        {
            if (Value == null)
                return false;

            MatchesPattern attrib = (MatchesPattern)ValidationAttribute;
            string pattern = attrib.Pattern;

            Regex regex = new Regex(pattern);
            return regex.IsMatch(Value.ToString());
        }
    }
}
